package com.example.index.arch.mvvm.model

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton

@Singleton
class IndexViewModelFactory
@Inject constructor(private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>) :
    ViewModelProvider.Factory {

    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val vm = creators[modelClass] ?: creators.entries.firstOrNull {
            it.key.isAssignableFrom(modelClass)
        }
        //unchecked cast
        return vm as T
    }


}